package com.ittx.dzt.shein.service.semi;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.OrderGetDTO;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.StoreConfigDTO;
import com.ittx.dzt.edi.vo.ResponseMessageVO;
import com.ittx.dzt.shein.handler.AbstractBaseSheinSemiApiHandler;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据订单号下载订单详情
 * <p>
 * cy.salesOrder.download.byOrderId
 * /open-api/order/order-detail 请求订单详情 https://open.sheincorp.com/zh/documents/apidoc/detail/3001317
 * </p>
 *
 * @author wl_code@163.com
 * @version 1.0
 * @date 2025/8/1
 */
@Slf4j
@Service
public class SheinSemiSalesOrderDownloadByIdService extends AbstractBaseSheinSemiApiHandler<OrderGetDTO> {


	@Autowired
	SheinSemiOrderAddressByIdService sheinSemiOrderAddressByIdService;

	@Override
	public String getLocalApiMethod() {
		return PlatformApiConstant.InboundInterface.SALESORDER_DOWNLOAD_BY_ORDERID;
	}

	@Override
	public Result<?> doProcess(PlatformApiParamDTO platformApiParamDTO, OrderGetDTO orderGetDTO) {
		// 参数校验
		Result<?> result = checkParams(platformApiParamDTO, orderGetDTO);
		if (result.hasError()) {
			return result;
		}
		List<Object> list = new ArrayList<>();
		String[] orderCodeArr = orderGetDTO.getOrderId().split(",");
		if (orderCodeArr.length > 0) {
			for (String code : orderCodeArr) {
				result = this.getOrderById(platformApiParamDTO, code);
				if (result.hasError()) {
					return result;
				}
				Object resultData = result.getData();
				// 同步返回
				if (orderGetDTO.getIsAsync() == 0) {
					list.add(resultData);
				} else {
					platformApiParamDTO.setPlatformIdentifierTag(code);
					result = this.sendRequestToPlatform(platformApiParamDTO, resultData, true);
					if (!result.isSuccess()) {
						return result;
					}
				}
			}
		}
		// 同步返回
		if (orderGetDTO.getIsAsync() == 0) {
			return Result.success().addData(list).addMsg(platformApiParamDTO.getStoreConfig().getChainUniqueCode());
		}
		return Result.success();
	}


	/**
	 * 根据id获取订单
	 *
	 * @param platformApiParamDTO
	 * @param id
	 * @return
	 */
	public Result<?> getOrderById(PlatformApiParamDTO platformApiParamDTO, String id) {
		StoreConfigDTO storeConfig = platformApiParamDTO.getStoreConfig();
		return this.getOrderBySdk(platformApiParamDTO, id);
	}

	/**
	 * 执行api请求
	 *
	 * @param platformApiParamDTO
	 * @param id
	 * @return 请求结果 resultMap.get(DATA_FIELD)
	 */
	public Result<?> getOrderBySdk(PlatformApiParamDTO platformApiParamDTO, String id) {
		Map<String, Object> params = new HashMap<>();
		params.put("orderNoList", ListUtil.of(id));
		StoreConfigDTO storeConfig = platformApiParamDTO.getStoreConfig();
		String requestId = platformApiParamDTO.getRequestId();
		Result<?> result = this.executeApi(storeConfig, params, requestId);
		if (result.hasError()) {
			return result;
		}
		Map<String, Object> resultMap = (Map<String, Object>) result.getData();
		Map<String, Object> saleData = (Map<String, Object>) resultMap.get(DATA_FIELD);
		if (MapUtil.isEmpty(saleData)) {
			String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0009_MSG, requestId, id);
			log.error(msg);
			return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0009, msg));
		}
		//根据订单号查询地址,组装到订单上
		Result<?> addressResult = sheinSemiOrderAddressByIdService.doProcess(platformApiParamDTO, id);
		if (addressResult.hasError()) {
			saleData.put("cyAutoGetAddressStatus", false);
			saleData.put("cyAutoGetAddress", JacksonTool.toJson(addressResult));
		} else {
			saleData.put("cyAutoGetAddressStatus", true);
			saleData.put("cyAutoGetAddress", addressResult.getData());
		}
		return Result.success().addData(saleData);
	}

	/**
	 * 参数校验
	 *
	 * @param platformApiParamDTO 平台请求参数
	 * @param orderGetDTO         接口请求参数
	 */
	public Result<?> checkParams(PlatformApiParamDTO platformApiParamDTO, OrderGetDTO orderGetDTO) {
		if (orderGetDTO == null || platformApiParamDTO.getStoreConfig() == null || StrUtil.isBlank(orderGetDTO.getOrderId())) {
			String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0004_MSG, platformApiParamDTO.getRequestId(),
					this.getPlatformCode(), getLocalApiMethod(), orderGetDTO);
			log.error(msg);
			return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0004, msg));
		}
		return Result.success();
	}

	@Override
	public String getSheinSemiMethod() {
		return "/open-api/order/order-detail";
	}
}
